home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / archiver / zoo21src.zoo / zooadd.c < prev    next >
C/C++ Source or Header  |  1991-07-24  |  27KB  |  735 lines

  1. #ifndef LINT
  2. /* derived from: zooadd.c 2.34 88/08/15 10:53:11 */
  3. static char sccsid[]="$Source: g:/newzoo\RCS\zooadd.c,v $\n\
  4. $Id: zooadd.c,v 1.3 1991/07/24 23:47:04 bjsjr Rel $";
  5. #endif /* LINT */
  6.  
  7. /*
  8. Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
  9. (C) Copyright 1988 Rahul Dhesi -- All rights reserved
  10. (C) Copyright 1991 Rahul Dhesi -- All rights reserved
  11. */
  12. #include "options.h"
  13. /* Adds files specified in parameter-list to archive zoo_path. */
  14.  
  15. #define LONGEST    20                    /* assumed length of longest filename */
  16. #include "zoomem.h"             /* to define MAXADD */
  17. #include "zoo.h"
  18. #include "zooio.h"
  19. #include "various.h"
  20. #include "parse.h"
  21. #include "debug.h"
  22.  
  23. #include "portable.h"
  24.  
  25. #include "zoofns.h"
  26. #include "errors.i"
  27. extern int break_hit;
  28. extern int quiet;
  29.  
  30. void show_comment PARMS ((struct direntry *, ZOOFILE, int, char *));
  31. void dosname PARMS ((char *, char *));
  32. void modpath PARMS ((char *));
  33. void opts_add PARMS ((char *, int *, int *, int *, int *, int *, int *,
  34.                int *, int *, int *, int *, int *, int *, int *, int *));
  35. int ver_too_high PARMS ((struct zoo_header *));
  36. void get_comment PARMS ((struct direntry *, ZOOFILE, char *));
  37. #ifndef PORTABLE
  38. void copyfields PARMS ((struct direntry *, struct tiny_header *));
  39. #endif
  40. void storefname PARMS ((struct direntry *, char *, int));
  41. char *choosefname PARMS ((struct direntry *));
  42.  
  43. extern struct zoo_header zoo_header;
  44.  
  45. extern char file_leader[];
  46. extern unsigned int crccode;
  47.  
  48. void zooadd(zoo_path, argc, argv, option)
  49. char *zoo_path;      /* pathname of zoo archive to add to */
  50. int argc;            /* how many filespecs supplied */
  51. char **argv;         /* array of pointers to filespecs */
  52. char *option;        /* option string */
  53. {
  54. char *whichname;                          /* which name to show user */
  55. char **flist;                                  /* list of ptrs to input fnames */
  56. int fptr;                                 /* will point to within flist */
  57. ZOOFILE this_file;                        /* file to add */
  58. char zoo_fname[LFNAMESIZE];               /* basename of archive itself */
  59. char zoo_bak[LFNAMESIZE];                 /* name of archive's backup */
  60. char this_fname[LFNAMESIZE];              /* just filename of file to add */
  61. char latest_name[LFNAMESIZE];             /* latest name in archive */
  62. long last_old = 0L;                       /* last direntry in old chain */
  63. ZOOFILE zoo_file;                         /* stream for open archive */
  64. char *this_path;                          /* pathname of file to add */
  65.  
  66. #ifdef NOENUM
  67. #define NEW_ZOO 1
  68. #define OLD_ZOO 2
  69. int zoo_status;
  70. #else
  71. enum {NEW_ZOO, OLD_ZOO} zoo_status;       /* newly created or not */
  72. #endif
  73.  
  74. long this_dir_offset;                     /* pointers to within archive */
  75. long save_position;                       /* pointer to within archive */
  76. long prev_pos;                            /* posn of prev file of same name */
  77. struct direntry direntry;                 /* directory entry */
  78. struct direntry dir2entry;                /* spare */
  79. int status;                               /* error status */
  80. int success;                              /* successful addition of file? */
  81. int addcount = 0;                         /* number added */
  82. int update=0;                             /* only files already in archive */
  83. int suppress=0;                           /* suppress compression */
  84. int new=0;                                /* add only files not in archive */
  85. int zootime = 0;                          /* just set archive time */
  86. int add_comment = 0;                      /* add comment */
  87. int add_global_comment = 0;                    /* archive comment */
  88. int pack = 0;                             /* pack after adding */
  89. int need_dir = 1;                         /* store directories too */
  90. int delcount = 0;                         /* count of deleted entries */
  91. int exit_status = 0;                      /* exit status to set */
  92.  
  93. unsigned int latest_date = 0;             /* to set time on archive itself */
  94. unsigned int latest_time = 0;             /* .. same */
  95. int move = 0;                             /* delete after adding to archive */
  96. int longest;                              /* length of longest pathname added */
  97. int firstfile = 1;                        /* first file being added? */
  98. int z_fmt = 0;                            /* look for Z format files? */
  99. int inargs = 0;                                    /* read filenames from stdin? */
  100.  
  101. #ifndef PORTABLE
  102. struct tiny_header tiny_header;           /* for Z format archives */
  103. #endif
  104.  
  105. unsigned this_version_no;                            /* version no. of old file */
  106. unsigned  high_vflag;                                /* version flag of old file */
  107. unsigned high_version_no;                            /* highest version no of this file */
  108. long high_pos;                                        /* offset of file w/highest ver no */
  109. unsigned int fgens;                                /* gens. to preserve -- file */
  110. unsigned int zgens;                                /* gens. to preserve -- archive */
  111. long oldcmtpos;                                    /* to save old comment */
  112. unsigned int oldcmtsiz;                            /* to save old comment */
  113. int genson = 0;                                    /* whether to turn generations on */
  114.  
  115. int use_lzh = 0;                                    /* whether to use lzh compression */
  116.  
  117. /* on entry option points to first letter */
  118.  
  119. opts_add (option, &zootime, &quiet, &suppress, &move, &new, &pack,
  120.           &update, &add_comment, &z_fmt, &need_dir, &inargs, &genson,
  121.              &use_lzh, &add_global_comment);
  122.  
  123. /* POSSIBLE RACE CONDITION BETWEEN TESTING EXISTENCE AND CREATING FILE */
  124. if (exists (zoo_path)) {
  125.     zoo_file = zooopen (zoo_path, Z_RDWR);
  126.    zoo_status = OLD_ZOO;
  127. } else {
  128.    if (!zootime)
  129.         zoo_file = zoocreate (zoo_path);
  130.    else
  131.       zoo_file = NOFILE;     /* don't create if just setting time */
  132.    zoo_status = NEW_ZOO;
  133. }
  134.  
  135. if (zoo_file == NOFILE)
  136.    prterror ('f', could_not_open, zoo_path);
  137. basename(zoo_path, zoo_fname);      /* get basename of archive */
  138. rootname (zoo_path, zoo_bak);       /* name without extension */
  139. strcat (zoo_bak, BACKUP_EXT);       /* name of backup of this archive */
  140.  
  141. /* Now we prepare the archive for adding one or more files.  If the archive
  142. has just been created, we write the archive header */
  143.  
  144. addfname ("",0L,0,0,0,0); /* initialize table of files already in archive */
  145. if (zoo_status == NEW_ZOO) {                 /* newly-created archive */
  146.     if (genson)                                            /* if no generations needed */
  147.         zoo_header.vdata = (VFL_ON|GEN_DEFAULT); /* generations on */
  148.    fwr_zooh (&zoo_header, zoo_file);
  149.     zgens = GEN_DEFAULT;
  150.    zooseek (zoo_file, zoo_header.zoo_start, 0); /* seek to where data begins */
  151. } else {
  152.    /* read header and rewrite with updated version numbers, preserving
  153.        header type */
  154.    rwheader (&zoo_header, zoo_file, 1);
  155.     zgens = zoo_header.vdata & VFL_GEN;            /* get archive generations */
  156.    /* initialize latest_name to null string */
  157.     /* NOTE:  latest_name is not currently used for anything, but
  158.         may be used in the future for inserting files into the
  159.         archive in alphabetic order. */
  160.    *latest_name = '\0';
  161.  
  162.    /* Skip existing files but add them to a list.  The variable last_old 
  163.    gets the tail of the old chain of directory entries */
  164.    skip_files (zoo_file, &latest_date, &latest_time, &delcount, 
  165.                latest_name, &last_old);
  166. }
  167.  
  168. /* The file pointer is now positioned correctly to add a file to archive, 
  169. unless the null directory entry is too short.  This will be fixed below. */
  170.  
  171. /* If we are just setting time, do it and run. */
  172. if (zootime) {
  173. #ifdef NIXTIME
  174.    zooclose (zoo_file);
  175.    setutime (zoo_path, latest_date, latest_time);
  176. #else
  177.    settime (zoo_file, latest_date, latest_time);
  178.    zooclose (zoo_file);
  179. #endif
  180.    prterror ('m', "Archive time adjusted.\n");
  181.    zooexit (0);
  182. }
  183.  
  184. /* make list of files, excluding archive and its backup */
  185. longest = LONGEST;
  186. flist = (char **) ealloc(MAXADD);
  187.  
  188. if (!inargs) {
  189.    makelist(argc, argv, flist, MAXADD-2, zoo_fname, zoo_bak, ".", &longest);
  190.    /*                                    ^^         ^^       ^^ exclude */
  191. }
  192.  
  193. fptr = 0;    /* ready to get filename (if makelist() was called) or to
  194.                     begin adding filenames (if reading them from stdi